/**
 * 
 */
package com.hs.params;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;

import com.hs.core.db.DBMgr;

/**
 * @author Administrator
 *
 */
public class ParamsCacheUtil {
    private static ParamsCacheUtil inst = null;
    /**
     * 查询所有paramtype
     */
    private static String DICTTYPESQL = "SELECT DICTTYPE FROM PDMS_CODE WHERE YOUXBJ='0' GROUP BY DICTTYPE ";
    
    private static String DICTSQL = "SELECT * FROM PDMS_CODE WHERE YOUXBJ='0' and  DICTTYPE =? order by ORDNUM";

    private Map dataMap; 
    
    private Map menuMap;//系统菜单
    
    private Map keywordMap;
     

    
    
    DBMgr db = null;
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		

	}
	public static void initInst(ServletContext context){
		inst = new ParamsCacheUtil(context);
	}
	private ParamsCacheUtil(ServletContext context){
		//dataMap = new HashMap<String, Map>();
		//menuMap = new HashMap<String, Map>(); 
		keywordMap = new HashMap<String,String>();
		init(context);
	}
	public static ParamsCacheUtil getInst(){
		return inst;
	}
	private void init(ServletContext context){ 
		db = DBMgr.getDbUtilInst(context);
//		List dataList = db.queryList(DICTTYPESQL, null);
//		if(dataList != null && dataList.size()>0){
//			for(int i =0;i<dataList.size();i++){
//				Map data = (Map)dataList.get(i);
//				String dictType = data.get("DICTTYPE")+"";
//				List dictList = db.queryList(DICTSQL, new Object[]{dictType});
//				dataMap.put(dictType, dictList); 
//			}
//		} 
		
		//initMenu(db); 
	}
	private void initKeyword(DBMgr dbo){
		List dataList = dbo.queryList("select ID,TAGNAME,TAGTYPE  from CHD_TAGS where pid>0 ", null);
		for(int i = 0;i<dataList.size();i++){
			Map m = (Map)dataList.get(i);
			String idStr = (String)m.get("TAGNAME");
			keywordMap.put(idStr, m);
		}
	}
	public Map getIdBytagName(String str){
		if(StringUtils.isEmpty(str))
			return null;
		return (Map)keywordMap.get(str);
	}
	public String getCodeNameByCode(String codevalue,String codetype){
		String str = "";
		List codeList = (List)dataMap.get(codetype);
		for(int i=0;i<codeList.size();i++){
			Map m = (Map)codeList.get(i);
			String code = (String)m.get("DICTCODE");
			if(codevalue.equals(code)){
				str = (String)m.get("DICTNAME");
				break;
			}
		}
		return str;
	}
	/**
	 * 初始化加载系统菜单
	 * @param dbo
	 */
	private void initMenu(DBMgr dbo){
		List dataList = dbo.queryList("SELECT POWERNAME,PARENTID,ID FROM PDMS_POWER WHERE ISROOT=1 AND LEVELID<=3 AND ISUSE=0", null);
		for(int i = 0;i<dataList.size();i++){
			Map m = (Map)dataList.get(i);
			String idStr = m.get("ID")+"";
			menuMap.put(idStr, m);
		}
		
	}
 
	/**
	 * 根据菜单主键获得菜单
	 * @param idStr
	 * @return
	 */
	public Map getMenuMap(String idStr){
		if(menuMap != null){
			return (Map)menuMap.get(idStr);
		}
		return null;
	}
	/**
	 * 下拉框选择没有默认值
	 * @param colname
	 * @param style
	 * @param selected
	 * @param dictType
	 * @return
	 */
	public String selectStrNodefault(String colname,String style,String selected,String dictType,String onchangeevent){
		  StringBuffer buffer = new StringBuffer("<select ");
		  buffer.append("id=\"").append(colname).append("\" name=\"").append(colname).append("\"")
		  .append(" onchange=\"").append(onchangeevent).append("\"")
		  .append(" style=\"").append(style).append("\">");
		  List dataList = (List)this.dataMap.get(dictType);
		  if(null != dataList)
		  for(int i =0;i<dataList.size();i++){
			  Map m = (Map)dataList.get(i);
			  String dictCode = (String)m.get("DICTCODE");
			  String isdefault = m.get("ISDEFAULT")+"";

			  buffer.append("<option value=\"").append(dictCode).append("\" ");
			  if(StringUtils.isNotBlank(selected)){
				  if(selected.equals(dictCode)){
					   buffer.append(" selected");
				  }else{
					  if("1".equals(isdefault)){
						  buffer.append(" selected");
					  }
				  }
			  }else{
				  if("1".equals(isdefault)){
					  buffer.append(" selected");
				  }
			  }
			  buffer.append(">");
			  buffer.append(m.get("DICTNAME"));
			  buffer.append("</option>");
		  }
		  buffer.append("</select>");
		  return buffer.toString();
	  }
	/**
	 * 下拉框选择没有默认值
	 * @param colname
	 * @param style
	 * @param selected
	 * @param dictType
	 * @return
	 */
	public String selectStrNodefault(String colname,String style,String selected,String dictType){
		  StringBuffer buffer = new StringBuffer("<select ");
		  buffer.append("id=\"").append(colname).append("\" name=\"").append(colname).append("\"")
		  
		  .append(" style=\"").append(style).append("\">");
		  List dataList = (List)this.dataMap.get(dictType);
		  if(null != dataList)
		  for(int i =0;i<dataList.size();i++){
			  Map m = (Map)dataList.get(i);
			  String dictCode = (String)m.get("DICTCODE");
			  String isdefault = m.get("ISDEFAULT")+""; 
			  buffer.append("<option value=\"").append(dictCode).append("\" ");
			  if(StringUtils.isNotBlank(selected)){
				  if(selected.equals(dictCode)){
					   buffer.append(" selected");
				  }else{
					  if("1".equals(isdefault)){
						  buffer.append(" selected");
					  }
				  }
			  }else{
				  if("1".equals(isdefault)){
					  buffer.append(" selected");
				  }
			  }
			  buffer.append(">");
			  buffer.append(m.get("DICTNAME")==null?"":m.get("DICTNAME"));
			  buffer.append("</option>");
		  }
		  buffer.append("</select>");
		  return buffer.toString();
	  }
	  /**
	   * 下拉框选择有默认值
	   * @param colname
	   * @param style
	   * @param selected
	   * @param dicttype
	   * @return
	   */
	  public String selectStrWithDefault(String colname,String style,String selected,String dicttype){
		  StringBuffer buffer = new StringBuffer("<select ");
		  buffer.append("id=\"").append(colname).append("\" name=\"").append(colname).append("\"")
		  .append(" style=\"").append(style).append("\">");
		  List dataList = (List)this.dataMap.get(dicttype);
		  if(null != dataList)
		  for(int i =0;i<dataList.size();i++){
			  Map m = (Map)dataList.get(i);
			  String dictCode = (String)m.get("DICTCODE");
			  if(i == 0)
			  buffer.append("<option value=\"0\">---请选择---</option>");
			  buffer.append("<option value=\"").append(dictCode).append("\" ");
			  if(StringUtils.isNotBlank(selected) && selected.equals(dictCode))
				   buffer.append(" selected");
			  buffer.append(">");
			  buffer.append(m.get("DICTNAME"));
			  buffer.append("</option>");
		  }
		  buffer.append("</select>");
		  return buffer.toString();
	  }
	  /**
	   * 根据选中的代码获取显示
	   * @param selected
	   * @param dicttype
	   * @return
	   */
	  public String selectStr4Display(String selected,String dicttype){ 
		  String str = "";
		 
		  List dataList = (List)this.dataMap.get(dicttype); 
		  if(null != dataList)
			  for(int i =0;i<dataList.size();i++){
				  Map m = (Map)dataList.get(i);
				  String dictCode = (String)m.get("DICTCODE");
				  String dictName = (String)m.get("DICTNAME"); 
				  if(StringUtils.isNotBlank(selected) && selected.equals(dictCode))
					   str = dictName==null?"":dictName;
			  }
		return str;
	  }
	  /**
	   * 复选框
	   * @param context
	   * @param colname
	   * @param style
	   * @param selectedStr
	   * @param dicttype
	   * @return
	   */
	  public String selectStrCheck(String colname,String style,String selectedStr,String dicttype){
		  StringBuffer buffer = new StringBuffer("");
		  Map selectedMap = new HashMap();
		  if(null != selectedStr){
			  String[] arr = selectedStr.split(",");
			  for(int j =0;j<arr.length;j++){
				  String str = arr[j];
				  selectedMap.put(str, str);
			  }
		  }
		  List dataList = (List)this.dataMap.get(dicttype);
		  if(null != dataList)
		  for(int i =0;i<dataList.size();i++){
			  Map m = (Map)dataList.get(i);
			  String dictCode = (String)m.get("DICTCODE");
			  String name = (String)m.get("DICTNAME");
			  buffer.append("<input type=\"checkbox\" value=\"");
			  buffer.append(dictCode);
			  buffer.append("\" name=\"");
			  buffer.append(colname+"\"");
			  if(null != selectedMap.get(dictCode))
				  buffer.append(" checked/>");
			  else
				  buffer.append("/>");
			  buffer.append(name);
			  buffer.append("&nbsp;");
		  } 
		  return buffer.toString();
	  }
}
